<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
  <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" th:href="@{/component/pear/css/pear.css}"/>
  </head>
  <body class="pear-container">
    <div class="layui-card">
      <div class="layui-card-body">
        <form class="layui-form" action="">
          <div class="layui-form-item">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-inline">
              <input type="text" name="menuName" placeholder="" class="layui-input">
            </div>
            <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="power-query">
              <i class="layui-icon layui-icon-search"></i>
              查询
            </button>
            <button type="reset" class="pear-btn pear-btn-md">
              <i class="layui-icon layui-icon-refresh"></i>
              重置
            </button>
          </div>
        </form>
      </div>
    </div>
    <div class="layui-card">
      <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
      </div>
    </div>

    <script type="text/html" id="power-toolbar">
      <button class="pear-btn pear-btn-primary pear-btn-md" sa:hasPermission="sys:menu:add" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
      </button>
      <button
          class="pear-btn pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
      </button>
    </script>

    <script type="text/html" id="power-bar">
      <button
          class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>
      </button>
      <button
          class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
        <i class="layui-icon layui-icon-delete"></i>
      </button>
    </script>

    <script type="text/html" id="power-type">
      {{#if (d.menuType == '0') { }}
      <span>目录</span>
      {{# }else if(d.menuType == '1'){ }}
      <span>菜单</span>
      {{# }else if(d.menuType == '2'){ }}
      <span>按钮</span>
      {{# } }}
    </script>

    <script type="text/html" id="power-enable">
      <input type="checkbox" name="enable" value="{{d.menuId}}" lay-skin="switch" lay-text="启用|禁用"
             lay-filter="power-enable" {{ d.enable== 1 ? 'checked' : '' }}>
    </script>

    <script type="text/html" id="icon">
      <i class="layui-icon {{d.icon}}"></i>
    </script>

  </body>
  <script th:src="@{/component/layui/layui.js}"></script>
  <script th:src="@{/component/pear/pear.js}"></script>
  <script th:src="@{/component/my/const.js}"></script>
  <script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'popup'], function () {
      let table = layui.table;
      let form = layui.form;
      let $ = layui.jquery;
      let treetable = layui.treetable;
      let popup = layui.popup;

      let MODULE_PATH = "/system/power/";

      window.render = function () {
        treetable.render({
          treeColIndex: 1,
          treeSpid: 0,
          treeIdName: 'menuId',
          treePidName: 'parentId',
          skin: 'line',
          method: 'post',
          treeDefaultClose: true,
          toolbar: '#power-toolbar',
          elem: '#power-table',
          url: suffix + "/menu/list",
          page: false,
          cols: [
            [
              {type: 'checkbox'},
              {field: 'menuName', minWidth: 200, title: '权限名称'},
              {field: 'icon', title: '图标', templet: '#icon'},
              {field: 'menuType', title: '权限类型', templet: '#power-type', align: 'center'},
              {field: 'enable', title: '是否可用', templet: '#power-enable'},
              {field: 'sort', title: '排序', align: 'center'},
              {title: '操作', templet: '#power-bar', width: 150, align: 'center'}
            ]
          ]
        });
      }

      render();

      form.on('submit(power-query)', function (data) {
        var keyword = data.field.menuName;
        var $tds = $('#power-table').next('.treeTable').find('.layui-table-body tbody tr td');
        if (!keyword) {
          $tds.css('background-color', 'transparent');
          layer.msg("请输入关键字", {icon: 5});
          return;
        }
        var searchCount = 0;
        $tds.each(function () {
          $(this).css('background-color', 'transparent');
          if ($(this).text().indexOf(keyword) >= 0) {
            $(this).css('background-color', 'rgba(250,230,160,0.5)');
            if (searchCount == 0) {
              $('body,html').stop(true);
              $('body,html').animate({scrollTop: $(this).offset().top - 150}, 500);
            }
            searchCount++;
          }
        });
        if (searchCount == 0) {
          layer.msg("没有匹配结果", {icon: 5});
        } else {
          treetable.expandAll('#power-table');
        }
        return false;
      });

      table.on('tool(power-table)', function (obj) {
        if (obj.event === 'remove') {
          window.remove(obj);
        } else if (obj.event === 'edit') {
          window.edit(obj);
        }
      })

      table.on('toolbar(power-table)', function (obj) {
        if (obj.event === 'add') {
          window.add();
        } else if (obj.event === 'refresh') {
          window.refresh();
        } else if (obj.event === 'batchRemove') {
          window.batchRemove(obj);
        }
      });

      form.on('switch(power-enable)', function (obj) {
        let operate;
        if (obj.elem.checked) {
          operate = "enable";
        } else {
          operate = "disable";
        }
        let loading = layer.load();
        $.ajax({
          url: '/menu/' + operate,
          data: {menuId: this.value},
          dataType: 'json',
          type: 'put',
          success: function (result) {
            layer.close(loading);
            if (result.success) {
              popup.success(result.msg);
            } else {
              popup.failure(result.msg);
            }
          }
        })
      });


      window.add = function () {
        layer.open({
          type: 2,
          title: '新增',
          shade: 0.3,
          area: ['455px', '565px'],
          content: '/menu/add'
        });
      }

      window.edit = function (obj) {
        layer.open({
          type: 2,
          title: '修改',
          shade: 0.3,
          area: ['450px', '560px'],
          content: suffix + '/menu/edit?menuId=' + obj.data['menuId']
        });
      }

      window.remove = function (obj) {
        layer.confirm('确定要删除该权限', {icon: 3, title: '提示'}, function (index) {
          layer.close(index);
          let loading = layer.load();
          $.ajax({
            url: suffix + "/menu/deleteOne",
            dataType: 'json',
            data: {menuId: obj.data['menuId']},
            type: 'delete',
            success: function (res) {
              layer.close(loading);
              if (res.success) {
                popup.success(res.msg, function () {
                  obj.del();
                })
              } else {
                popup.failure(res.msg);
              }
            }
          })
        });
      }
    })
  </script>
</html>
